package core.manager
{
import core.model.GameModel;
import core.model.MapModel;

import flash.geom.Point;

import isoengine.interfaces.IMapSprite;

import mapengine.building.MockMapBuilding;
import mapengine.interfaces.IIsoMap;
import mapengine.interfaces.IMapBuilding;

import values.item.BuildingItemValue;

/**
 * MapBuildingManager
 * @author jliang
 *
 */
public class MapBuildingManager
{
    private static function get mapModel():MapModel
    {
        return GameModel.instance.mapModel;
    }
    
	private static var world:IIsoMap;
	
	public static function setWorld(value:IIsoMap):void
	{
		world = value;
	}
	
	public static function stash(building:IMapBuilding):void
	{
        mapModel.stashBuilding(building.buildingItemValue);
		
		if (building.world)
		{
			building.world.removeSprite(building);
			building.dispose();
		}
	}
    
    public static function sell(building:IMapBuilding):void
    {
        mapModel.sellBuilding(building.buildingItemValue);
        
        if (building.world)
        {
            building.world.removeSprite(building);
            building.dispose();
        }
    }
    
    public static function rotate(building:IMapBuilding):void
    {
        if ( building.changeNextDirection() >= 0)
        {
            mapModel.rotateBuilding(building.buildingItemValue);
        }
    }
	
	public static function move(building:IMapBuilding):void
	{
		var moveCompleteHandler:Function = function(movedBuilding:IMapSprite):void
		{
			if (movedBuilding.objectData.x != nowPoint.x || movedBuilding.objectData.y != nowPoint.y)
            {
                mapModel.moveBuilding(building.buildingItemValue);
            }
		};
		
        var nowPoint:Point = new Point(building.position.x, building.position.y);        
		world.dragSprite(building, moveCompleteHandler);
	}
	
	public static function create(data:BuildingItemValue, callBack:Function = null):void
	{
		var createCompleteHandler:Function = function(mockObj2:IMapSprite):void
		{
			world.removeSprite(mock);
			mock.dispose();
			mock = null;
			
			var building:IMapBuilding = world.createBuilding(data);
			world.addSprite(building);
			
            mapModel.createBuilding(data);
            
            if (callBack != null)
            {
                callBack(building);
            }
		};
		
		var mockBuildingAssetReady:Function = function(mockObj1:IMapBuilding):void
		{
			world.addSprite(mock);
			world.dragSprite(mock, createCompleteHandler);
		};
		
		var mock:MockMapBuilding = new MockMapBuilding();
		mock.assetReadyCallBack = mockBuildingAssetReady;
		mock.objectData = data;
		mock.initialize();
	}
    
    public static function cancelDrag():void
    {
        
    }
}
}



